/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: BaseDao
 * Author:   步步为莹
 * Date:     2019/8/17 20:55
 * Description: 数据库工具类
 * History:
 */
package cn.beyond.vehicleManagement.dao;

import cn.beyond.vehicleManagement.util.ConfigureManager;

import java.sql.*;

/**
 * 〈数据库工具类〉<br> 
 * 〈一句话功能简述〉
 *
 * @author 步步为莹
 * @create 2019/8/17
 * @since 1.0.0
 */
public class BaseDao {
    //数据库连接信息
    private static  final String DRIVER= ConfigureManager.getProperty("driver");//驱动
    private static  final String URL= ConfigureManager.getProperty("url");//数据库URL
    private static  final  String USERNAME= ConfigureManager.getProperty("username");//用户名
    private static  final  String PASSWORD= ConfigureManager.getProperty("password");//密码
    // 四个jdbc数据操纵相关对象
    Connection conn=null;//连接
    ResultSet rs=null;//数据库结果集
    Statement stmt=null;
    PreparedStatement pstmt=null;

    /**
     * 数据库连接方法
     * @return
     */
    private Connection getConnection(){
        try {
            Class.forName(DRIVER);
             conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;

    }

    /**
     * 处理查询通用方法
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql,Object...params){
        this.getConnection();
        if(conn!=null){
            try {
            if(params.length==0){
                stmt=conn.createStatement();
                rs=stmt.executeQuery(sql);
                } else {
                pstmt=conn.prepareStatement(sql);
                for (int i=0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
               rs=pstmt.executeQuery();
            }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
       return  rs;
    }

    /**
     * 处理增删改的通用方法
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String  sql,Object...params ){
        this.getConnection();
        if(conn!=null){
            try {
                if(params.length==0){
                    stmt=conn.createStatement();
                   return  stmt.executeUpdate(sql);
                } else {
                    pstmt=conn.prepareStatement(sql);
                    for (int i=0;i<params.length;i++){
                        pstmt.setObject(i+1,params[i]);
                    }
                   return pstmt.executeUpdate();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  -1;
    }

    /**
     * 关闭数据库资源
     */
    public  void closeAll(){
        try{
            if(rs!=null&&!rs.isClosed()){
             rs.close();;
            }
            if(stmt!=null&&!stmt.isClosed()){
                stmt.close();
            }
            if(pstmt!=null&&pstmt.isClosed()){
                pstmt.close();
            }
            if (conn != null && conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


}